#pragma once

#include <memory>
#include <string>

// 版本控制类型
enum class VcsType
{
    TortoiseSVN = 0,
    TortoiseGIT = 1,
    SVN = 2,  // Native CLI
    GIT = 3   // Native CLI
};

// 抽象接口：版本控制客户端
class IVersionControlClient
{
public:
    virtual ~IVersionControlClient() = default;

    virtual void Update(const std::string& workingCopyPath) = 0;
    virtual void Commit(const std::string& workingCopyPath) = 0;
    virtual void Log(const std::string& workingCopyPath) = 0;
    virtual void Revert(const std::string& workingCopyPath) = 0;
    virtual void Cleanup(const std::string& workingCopyPath) = 0;
    // 对于 Git，source 可忽略；对于 SVN 使用 fromurl
    virtual void Merge(const std::string& source, const std::string& targetWorkingCopyPath) = 0;
};

// TortoiseSVN 实现
class TortoiseSvnClient final : public IVersionControlClient
{
public:
    explicit TortoiseSvnClient(std::string tortoiseProcPath);

    void Update(const std::string& workingCopyPath) override;
    void Commit(const std::string& workingCopyPath) override;
    void Log(const std::string& workingCopyPath) override;
    void Revert(const std::string& workingCopyPath) override;
    void Cleanup(const std::string& workingCopyPath) override;
    void Merge(const std::string& source, const std::string& targetWorkingCopyPath) override;

private:
    std::string m_exe; // 可为 "TortoiseProc.exe" 或配置路径
    void RunCommand(const std::string& command, const std::string& path);
};

// TortoiseGit 实现
class TortoiseGitClient final : public IVersionControlClient
{
public:
    explicit TortoiseGitClient(std::string tortoiseGitProcPath);

    void Update(const std::string& workingCopyPath) override;  // 映射为 /command:pull
    void Commit(const std::string& workingCopyPath) override;
    void Log(const std::string& workingCopyPath) override;
    void Revert(const std::string& workingCopyPath) override;
    void Cleanup(const std::string& workingCopyPath) override;
    void Merge(const std::string& source, const std::string& targetWorkingCopyPath) override; // 打开合并对话框

private:
    std::string m_exe; // 可为 "TortoiseGitProc.exe" 或配置路径
    void RunCommand(const std::string& command, const std::string& path);
};

// 原生 SVN CLI 实现
class SvnCliClient final : public IVersionControlClient
{
public:
    explicit SvnCliClient(std::string svnExePath);

    void Update(const std::string& workingCopyPath) override;
    void Commit(const std::string& workingCopyPath) override;
    void Log(const std::string& workingCopyPath) override;
    void Revert(const std::string& workingCopyPath) override;
    void Cleanup(const std::string& workingCopyPath) override;
    void Merge(const std::string& source, const std::string& targetWorkingCopyPath) override;

private:
    std::string m_exe; // 可为 "svn" 或自定义路径
    void RunConsole(const std::string& commandLine);
};

// 原生 Git CLI 实现
class GitCliClient final : public IVersionControlClient
{
public:
    explicit GitCliClient(std::string gitExePath);

    void Update(const std::string& workingCopyPath) override;
    void Commit(const std::string& workingCopyPath) override;
    void Log(const std::string& workingCopyPath) override;
    void Revert(const std::string& workingCopyPath) override;
    void Cleanup(const std::string& workingCopyPath) override;
    void Merge(const std::string& source, const std::string& targetWorkingCopyPath) override;

private:
    std::string m_exe; // 可为 "git" 或自定义路径
    void RunConsoleInRepo(const std::string& repoPath, const std::string& inlineCommands);
};

// 工厂/获取实例
namespace VersionControlFactory
{
    std::unique_ptr<IVersionControlClient> Create(VcsType type,
                                                  const std::string& tortoiseSvnPath,
                                                  const std::string& tortoiseGitPath,
                                                  const std::string& svnCliPath,
                                                  const std::string& gitCliPath);
}

